package com.lxmes.business.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.business.mapper.NcQualityGoalAssessmentMapper;
import com.lxmes.business.domain.NcQualityGoalAssessment;
import com.lxmes.business.service.INcQualityGoalAssessmentService;

/**
 * 质量目标考核Service业务层处理
 * 
 * @author lx
 * @date 2023-06-01
 */
@Service
public class NcQualityGoalAssessmentServiceImpl implements INcQualityGoalAssessmentService 
{
    @Autowired
    private NcQualityGoalAssessmentMapper ncQualityGoalAssessmentMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询质量目标考核
     * 
     * @param id 质量目标考核主键
     * @return 质量目标考核
     */
    @Override
    public NcQualityGoalAssessment selectNcQualityGoalAssessmentById(Long id)
    {
        return ncQualityGoalAssessmentMapper.selectNcQualityGoalAssessmentById(id);
    }

    /**
     * 查询质量目标考核列表
     * 
     * @param ncQualityGoalAssessment 质量目标考核
     * @return 质量目标考核
     */
    @Override
    public List<NcQualityGoalAssessment> selectNcQualityGoalAssessmentList(NcQualityGoalAssessment ncQualityGoalAssessment)
    {
        return ncQualityGoalAssessmentMapper.selectNcQualityGoalAssessmentList(ncQualityGoalAssessment);
    }

    /**
     * 新增质量目标考核
     * 
     * @param ncQualityGoalAssessment 质量目标考核
     * @return 结果
     */
    @Override
    public int insertNcQualityGoalAssessment(NcQualityGoalAssessment ncQualityGoalAssessment)
    {
        ncQualityGoalAssessment.setCreateTime(DateUtils.getNowDate());
        return ncQualityGoalAssessmentMapper.insertNcQualityGoalAssessment(ncQualityGoalAssessment);
    }

    /**
     * 修改质量目标考核
     * 
     * @param ncQualityGoalAssessment 质量目标考核
     * @return 结果
     */
    @Override
    public int updateNcQualityGoalAssessment(NcQualityGoalAssessment ncQualityGoalAssessment)
    {
        ncQualityGoalAssessment.setUpdateTime(DateUtils.getNowDate());
        return ncQualityGoalAssessmentMapper.updateNcQualityGoalAssessment(ncQualityGoalAssessment);
    }

    /**
     * 批量删除质量目标考核
     * 
     * @param ids 需要删除的质量目标考核主键
     * @return 结果
     */
    @Override
    public int deleteNcQualityGoalAssessmentByIds(Long[] ids)
    {
        return ncQualityGoalAssessmentMapper.deleteNcQualityGoalAssessmentByIds(ids);
    }

    /**
     * 删除质量目标考核信息
     * 
     * @param id 质量目标考核主键
     * @return 结果
     */
    @Override
    public int deleteNcQualityGoalAssessmentById(Long id)
    {
        return ncQualityGoalAssessmentMapper.deleteNcQualityGoalAssessmentById(id);
    }
    @Override
    public String imporIteminfo(List<NcQualityGoalAssessment> ncQualityGoalAssessmentList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncQualityGoalAssessmentList) || ncQualityGoalAssessmentList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcQualityGoalAssessment ncQualityGoalAssessment : ncQualityGoalAssessmentList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncQualityGoalAssessment);
                ncQualityGoalAssessment.setCreateBy(operName);
                ncQualityGoalAssessment.setUpdateBy(operName);
                this.insertNcQualityGoalAssessment(ncQualityGoalAssessment);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
